package com.chixing.controller;


import com.chixing.entity.Dish;
import com.chixing.service.IDishService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */
@RestController  // @ResponseBody (类中的方法的数据是 json格式) + @Controller
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private IDishService dishService;

    // 根据id查询
    @GetMapping("{dishId}")
    public Result getById(@PathVariable("dishId") Integer dishId){
        return dishService.getById(dishId);
    }
    //根据typeid查找
    @GetMapping("/type/{typeId}")
    public Result getByTypeId(@PathVariable Integer typeId) {
        // 根据类型 ID 查询菜品
        Result result =dishService.getByTypeId(typeId);
        return result;
    }




    // 查询所有
    // http://localhost:8080/kfc/dish
    @GetMapping
    public Result getAll(){
        Result result = dishService.getAll();
        return result;
    }

    // 添加新菜品
    @PostMapping
    public Result save(@RequestBody Dish dish){
        dish.setCreateTime(LocalDateTime.now());
        dish.setDishStatus(0);
        dish.setDishImg("images/kfc_food3.png");
        return dishService.save(dish);
    }

    //  修改菜品
    @PutMapping
    public Result update(Dish dish){
        Result result = dishService.update(dish);
        return result;
    }

    //删除菜品  http://localhost:8080/kfc/dish/4
    @DeleteMapping("{dishId}")
    public Result delete(@PathVariable("dishId") Integer dishId){
        Result result = dishService.delete(dishId);
        return result;
    }

    //膜乎搜索

    @GetMapping("/name/{dishName}")
    public Result getByName(@PathVariable("dishName")String dishName){
        Result result = dishService.getByName(dishName);
        return result;
    }

}
